package com.mspbots.sync.wise.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.mspbots.sync.BaseGenericService;
import com.mspbots.sync.utils.WiseParamsUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * com.mspbots.sync.wise.service.PurchaseOrderService
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/10/17
 */
@Log4j2
@Service
public class PurchaseService extends BaseGenericService {

    public Mono<JsonNode> getPurchase(Map<String, String> queryParams) {

        Integer tenant = Integer.parseInt(queryParams.get("tenantId"));
        return this.onGetWiseProperties(tenant)
                .flatMap(configuration -> cwWebClient.getRepository("/schedule/entries",
                        WiseParamsUtils.mapToMultiValueMapBuild(queryParams))
                        .subscriberContext(ctx -> ctx.put("properties", configuration))
                        .flatMap(jsonNode -> {
                            if (ObjectUtils.isEmpty(jsonNode.get("id"))) {
                                log.error("This tenant sync getStatuses  error.msg: {}", jsonNode);
                                return Mono.empty();
                            }
                            return Mono.just(jsonNode);
                        }));

    }

}
